package cn.shadow.thread;

import org.junit.Test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadInterrupt {


    /**
     * 运行时中断
     */
    @Test
    public void testRunnable() {
        Thread thread = new Thread(() -> {
            while (!Thread.currentThread().isInterrupted()) {
                System.out.println("hello");
            }
            System.out.println("end");
        });
        thread.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread.interrupt();
    }


    /**
     * 等待状态中断
     */
    @Test
    public void testWaiting() {
        Thread thread = new Thread(() -> {

            try {
                System.out.println("1:" + Thread.currentThread().isInterrupted()); //false
                Thread.sleep(50000);
            } catch (InterruptedException e) {
                System.out.println("睡眠中断异常捕获");
                //抛出异常后中断状态位会清空
                System.out.println("2:" + Thread.currentThread().isInterrupted()); //false
                Thread.currentThread().interrupt(); //重新设置状态位
            }
            System.out.println("3:" + Thread.currentThread().isInterrupted()); //true
        });
        thread.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread.interrupt();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //线程执行完后，状态返回都是false
        System.out.println("4" + thread.isInterrupted()); //false

    }


    /**
     * 等待状态中断
     */
    @Test
    public void testWaiting2()  {
        Thread thread = new Thread(() -> {
            synchronized (this) {
                try {
                    System.out.println("1:" + Thread.currentThread().isInterrupted()); //false
                    wait();
                } catch (InterruptedException e) {
                    System.out.println("触发中断"); //中断状态清空
                    System.out.println("2:" + Thread.currentThread().isInterrupted()); //false
                    Thread.currentThread().interrupt(); //设置状态
                }
                System.out.println("3:" + Thread.currentThread().isInterrupted()); //true
            }
        });
        thread.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("中断线程");
        thread.interrupt();
        System.out.println("4:" + Thread.currentThread().isInterrupted()); //false
    }


    @Test
    public void testBlocking() throws InterruptedException {
        Object lock = new Object();
        Thread thread = new Thread(() -> {
            synchronized (lock) {  //如果阻塞了，线程调用了interrupt方法，只会使得标志位改变，并不会改变阻塞状态
                while (!Thread.currentThread().isInterrupted()) {

                }
                System.out.println("exit");
                lock.notify(); //通知
            }
        });

        synchronized (lock) {
            thread.start();
            thread.interrupt(); //设置中断
            lock.wait();  //等待
            thread.join(); //要等thread完成
        }
    }

    @Test
    public void testExecutors() {

        ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.shutdown();


    }
}
